package taskOnClasses;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.*;
import java.awt.Dimension;

public class CompanyGUI extends JPanel implements ActionListener {

    private Company myCompany = new Company("Irenka Komerc", "Posle MakAm, desno...");
    private Client someC = null;
    private String oldSurname = new String();
    private int oldID = 0;
    private JButton Add = new JButton("Add"),
            Find = new JButton("Find"),
            Remove = new JButton("Remove"),
            GenReport = new JButton("Generate Report"),
            bDeposit = new JButton("Deposit"),
            bWithdraw = new JButton("Withdraw"),
            bClear = new JButton("Clear"),
            bNotice = new JButton("Generate Notice"),
            bSave = new JButton("Save Changes");
    private JLabel Name = new JLabel("Name"),
            Surname = new JLabel("Surname"),
            Age = new JLabel("Age"),
            DOB = new JLabel("Date of Birth"),
            GenderButton = new JLabel("Gender"),
            AccessCode = new JLabel("Access Code"),
            AccountID = new JLabel("Account ID"),
            Balance = new JLabel("Balance"),
            Percentage = new JLabel("Percentage"),
            Status = new JLabel("Is Frozen"),
            Deposit = new JLabel("Deposit"),
            Withdraw = new JLabel("Withdraw");
    private JTextField fName = new JTextField(""),
            fSurname = new JTextField(""),
            fAge = new JTextField(""),
            fDOB = new JTextField(""),
            fGender = new JTextField(""),
            fAccessCode = new JTextField(""),
            fAccountID = new JTextField(""),
            fBalance = new JTextField(""),
            fPercentage = new JTextField(""),
            fStatus = new JTextField(""),
            fDeposit = new JTextField(""),
            fWithdraw = new JTextField(""),
            fStatusLine = new JTextField("");

    public CompanyGUI() {

        fStatus.setVisible(false);
        fWithdraw.setVisible(false);
        fDeposit.setVisible(false);
        bWithdraw.setVisible(false);
        bDeposit.setVisible(false);
        bSave.setVisible(false);
        bNotice.setVisible(false);

        Dimension textFields = new Dimension(70, 20);
        fName.setMinimumSize(textFields);
        fSurname.setMinimumSize(textFields);
        fAge.setMinimumSize(textFields);
        fDOB.setMinimumSize(textFields);
        fGender.setMinimumSize(textFields);
        fAccessCode.setMinimumSize(textFields);
        fAccountID.setMinimumSize(textFields);
        fBalance.setMinimumSize(textFields);
        fPercentage.setMinimumSize(textFields);
        fStatus.setMinimumSize(textFields);
        fDeposit.setMinimumSize(textFields);
        fWithdraw.setMinimumSize(textFields);

        Dimension statusLine = new Dimension(180, 20);
        fStatusLine.setMinimumSize(statusLine);
        fStatusLine.setEditable(false);

        //allow only digits and the decimal dot as input characters
        fAge.addActionListener(this);
        fAge.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fDOB.addActionListener(this);
        fDOB.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fAccessCode.addActionListener(this);
        fAccessCode.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fAccountID.addActionListener(this);
        fAccountID.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fBalance.addActionListener(this);
        fBalance.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fPercentage.addActionListener(this);
        fPercentage.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fDeposit.addActionListener(this);
        fDeposit.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });
        fWithdraw.addActionListener(this);
        fWithdraw.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((Character.isDigit(c)
                        || (c == KeyEvent.VK_PERIOD)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE)))) {
                    e.consume();
                }
            }
        });


        Add.setVerticalTextPosition(AbstractButton.CENTER);
        Add.setHorizontalTextPosition(AbstractButton.LEADING);
        Add.setActionCommand("add");
        Add.addActionListener(this);

        Find.setVerticalTextPosition(AbstractButton.CENTER);
        Find.setHorizontalTextPosition(AbstractButton.LEADING);
        Find.setActionCommand("find");
        Find.addActionListener(this);

        Remove.setVerticalTextPosition(AbstractButton.CENTER);
        Remove.setHorizontalTextPosition(AbstractButton.LEADING);
        Remove.setActionCommand("remove");
        Remove.addActionListener(this);

        GenReport.setVerticalTextPosition(AbstractButton.CENTER);
        GenReport.setHorizontalTextPosition(AbstractButton.LEADING);
        GenReport.setActionCommand("genrep");
        GenReport.addActionListener(this);

        bDeposit.setVerticalTextPosition(AbstractButton.CENTER);
        bDeposit.setHorizontalTextPosition(AbstractButton.LEADING);
        bDeposit.setActionCommand("deposit");
        bDeposit.addActionListener(this);

        bWithdraw.setVerticalTextPosition(AbstractButton.CENTER);
        bWithdraw.setHorizontalTextPosition(AbstractButton.LEADING);
        bWithdraw.setActionCommand("withdraw");
        bWithdraw.addActionListener(this);

        bClear.setVerticalTextPosition(AbstractButton.CENTER);
        bClear.setHorizontalTextPosition(AbstractButton.LEADING);
        bClear.setActionCommand("clear");
        bClear.addActionListener(this);

        bNotice.setVerticalTextPosition(AbstractButton.CENTER);
        bNotice.setHorizontalTextPosition(AbstractButton.LEADING);
        bNotice.setActionCommand("notice");
        bNotice.addActionListener(this);

        bSave.setVerticalTextPosition(AbstractButton.CENTER);
        bSave.setHorizontalTextPosition(AbstractButton.LEADING);
        bSave.setActionCommand("save");
        bSave.addActionListener(this);

        //the main container
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);

        layout.setAutoCreateGaps(true);//automatically create a bit of space between components

        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(Name).
                addComponent(Surname).
                addComponent(Age).
                addComponent(DOB).
                addComponent(GenderButton).
                addComponent(AccessCode).
                addComponent(AccountID).
                addComponent(Balance).
                addComponent(Percentage).
                addComponent(Status).
                addComponent(Deposit).
                addComponent(Withdraw).
                addComponent(fStatusLine));
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(fName).
                addComponent(fSurname).
                addComponent(fAge).
                addComponent(fDOB).
                addComponent(fGender).
                addComponent(fAccessCode).
                addComponent(fAccountID).
                addComponent(fBalance).
                addComponent(fPercentage).
                addComponent(fStatus).
                addComponent(fDeposit).
                addComponent(fWithdraw));
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(Add).
                addComponent(Find).
                addComponent(Remove).
                addComponent(bClear).
                addComponent(bSave).
                addComponent(bDeposit).
                addComponent(bWithdraw).
                addComponent(GenReport).
                addComponent(bNotice));
        layout.setHorizontalGroup(hGroup);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Name).addComponent(fName).addComponent(Add));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Surname).addComponent(fSurname).addComponent(Find));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Age).addComponent(fAge).addComponent(Remove));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(DOB).addComponent(fDOB).addComponent(bClear));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(GenderButton).addComponent(fGender).addComponent(bSave));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(AccessCode).addComponent(fAccessCode).addComponent(bDeposit));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(AccountID).addComponent(fAccountID).addComponent(bWithdraw));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Balance).addComponent(fBalance).addComponent(GenReport));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Percentage).addComponent(fPercentage).addComponent(bNotice));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Status).addComponent(fStatus));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Deposit).addComponent(fDeposit));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(Withdraw).addComponent(fWithdraw));
        vGroup.addGroup(layout.createParallelGroup().addComponent(fStatusLine));
        layout.setVerticalGroup(vGroup);

        panel.setLayout(layout);
        add(panel);


    }

    public void actionPerformed(ActionEvent evt) {



        if ("add".equals(evt.getActionCommand())) {
           // try {
                Client newClient = new Client(fName.getText(),
                        fSurname.getText(),
                        Integer.parseInt(fAge.getText()),
                        Integer.parseInt(fDOB.getText()),
                        fGender.getText(),
                        Integer.parseInt(fAccessCode.getText()),
                        Integer.parseInt(fAccountID.getText()),
                        Double.parseDouble(fBalance.getText()),
                        Double.parseDouble(fPercentage.getText()),
                        Boolean.parseBoolean(fStatus.getText()));
                try {
                myCompany.addClient(newClient);
                fStatusLine.setText("Client added to archive");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText(e.toString());
                fStatusLine.repaint();
            }
        } else if ("remove".equals(evt.getActionCommand())) {
            try {
                myCompany.removeClient(fSurname.getText(),
                        Integer.parseInt(fAccountID.getText()));
                fStatusLine.setText("Client removed from archive");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText("Client not removed");
                fStatusLine.repaint();
            }
        } else if ("find".equals(evt.getActionCommand())) {
            try {
                boolean found = myCompany.containsClient(fSurname.getText(),
                        Integer.parseInt(fAccountID.getText()));
                if (found) {
                    fStatusLine.setText("Client found");
                    fStatusLine.repaint();
                    someC = myCompany.findClient(fSurname.getText(),
                            Integer.parseInt(fAccountID.getText()));

                    oldSurname = someC.getSurname();
                    oldID = someC.getID();

                    fName.setText(someC.getName());
                    fDOB.setText(String.valueOf(someC.getDateOfBirth()));
                    fAge.setText(String.valueOf(someC.getAge()));
                    fGender.setText(String.valueOf(someC.getGender()));
                    fAccessCode.setText(String.valueOf(someC.getAccessCode()));
                    fBalance.setText(String.valueOf(someC.getBalance()));
                    fPercentage.setText(String.valueOf(someC.getPercentage()));
                    fStatus.setText(String.valueOf(someC.isFrozen()));

                    //make possibilities visible
                    fStatus.setVisible(true);
                    fWithdraw.setVisible(true);
                    fDeposit.setVisible(true);
                    bWithdraw.setVisible(true);
                    bDeposit.setVisible(true);
                    bSave.setVisible(true);
                    bNotice.setVisible(true);
                }
            } catch (Exception e) {

                fStatusLine.setText("Client not found");
                fStatusLine.repaint();
            }
        } else if ("genrep".equals(evt.getActionCommand())) {
            try {
                myCompany.generateReport();
                fStatusLine.setText("Report generated");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText("Report not generated");
                fStatusLine.repaint();
            }
        } else if ("deposit".equals(evt.getActionCommand())) {
            try {
                myCompany.depositForClient(fSurname.getText(),
                        Integer.parseInt(fAccountID.getText()),
                        Double.parseDouble(fDeposit.getText()));
                fStatusLine.setText("Ammount deposited");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText("Ammount not deposited");
                fStatusLine.repaint();
            }
        } else if ("withdraw".equals(evt.getActionCommand())) {
            try {
                myCompany.withdrawForClient(fSurname.getText(),
                        Integer.parseInt(fAccountID.getText()),
                        Double.parseDouble(fWithdraw.getText()));
                fStatusLine.setText("Ammount withdrawn");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText("Ammount not withdrawn");
                fStatusLine.repaint();
            }
        } else if ("clear".equals(evt.getActionCommand())) {
            fName.setText("");
            fSurname.setText("");
            fAccessCode.setText("");
            fAccountID.setText("");
            fAge.setText("");
            fBalance.setText("");
            fDOB.setText("");
            fDeposit.setText("");
            fGender.setText("");
            fPercentage.setText("");
            fWithdraw.setText("");
            fStatus.setText("");
            fStatusLine.setText("");

            fStatus.setVisible(false);
            fWithdraw.setVisible(false);
            fDeposit.setVisible(false);
            bWithdraw.setVisible(false);
            bDeposit.setVisible(false);
            bSave.setVisible(false);
            bNotice.setVisible(false);
        } else if ("notice".equals(evt.getActionCommand())) {
            try {
                someC = myCompany.findClient(fSurname.getText(),
                        Integer.parseInt(fAccountID.getText()));

                someC.generateReport();
                //myCompany.generateReport();
                fStatusLine.setText("Notice generated");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText("Report not generated");
                fStatusLine.repaint();
            }
        } else if ("save".equals(evt.getActionCommand())) {
            try {
                someC = new Client(fName.getText(),
                        fSurname.getText(),
                        Integer.parseInt(fAge.getText()),
                        Integer.parseInt(fDOB.getText()),
                        fGender.getText(),
                        Integer.parseInt(fAccessCode.getText()),
                        Integer.parseInt(fAccountID.getText()),
                        Double.parseDouble(fBalance.getText()),
                        Double.parseDouble(fPercentage.getText()),
                        Boolean.parseBoolean(fStatus.getText()));
                myCompany.changeClientData(oldSurname, oldID, someC);
                fStatusLine.setText("Changes saved");
                fStatusLine.repaint();
            } catch (Exception e) {
                fStatusLine.setText("Changes not saved");
                fStatusLine.repaint();
            }
        }
    }

    public void createAndShowGUI() {

        JFrame mainFrame = new JFrame("Company Clients Database");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension mainDimension = new Dimension(400, 440);
        mainFrame.setMinimumSize(mainDimension);

        mainFrame.add(new CompanyGUI());

        mainFrame.pack();
        mainFrame.setVisible(true);
    }
}
